package com.example.base.Utils;

import android.content.SharedPreferences;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

import static android.content.Context.MODE_PRIVATE;

public class CookieJarManager implements CookieJar {
  private final String TAG = "CookieJarManager";
  private List<Cookie> mcookies = new ArrayList<>();


  public List<Cookie> getMcookies() {
    return mcookies;
  }

  private static HashMap<String, List<Cookie>> cookieStore = new HashMap<>();

  @Override
  public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
    if (null == url || null == cookies || cookies.size() <= 0) {
      return;
    } else {
      mcookies.addAll(cookies);
    }

    cookieStore.put(url.host(), cookies);
    Log.i(TAG, cookieStore + "");
  }

  @Override
  public List<Cookie> loadForRequest(HttpUrl url) {
    if (null != url) {
      List<Cookie> cookies = cookieStore.get(url.host());
      return cookies != null ? cookies : new ArrayList<Cookie>();
    } else {
      return new ArrayList<Cookie>();
    }
  }


}
